import { getDefaultFields } from '../utils';

module.exports = app => {
	const DataTypes = app.Sequelize;

	const Model = app.model.define('cart', {
		id: {
			type: DataTypes.INTEGER(8).UNSIGNED,
			allowNull: false,
			primaryKey: true,
			autoIncrement: true
		},
		user_id: {
			type: DataTypes.STRING(50),
			allowNull: false,
			defaultValue: ''
		},
		goods_id: {
			type: DataTypes.INTEGER(8).UNSIGNED,
			allowNull: false,
			defaultValue: '0'
		},
		sku_id: {
			type: DataTypes.INTEGER(8).UNSIGNED,
			allowNull: true,
			defaultValue: '0'
		},
		number: {
			type: DataTypes.INTEGER(5).UNSIGNED,
			allowNull: false,
			defaultValue: 0
		},
		...getDefaultFields()
	}, {
		tableName: 'cart',
		timestamps: false
	});

	Model.sync({ alter: true });
	
	Model.associate = function() {
		// app.model.Cart.hasOne(app.model.GoodsSku, {
        //     foreignKey: 'id',
        //     constraints: false,
		// 	sourceKey: 'sku_id'
		// });
		app.model.Cart.hasOne(app.model.Goods, {
            foreignKey: 'id',
            constraints: false,
			sourceKey: 'goods_id'
		});
	}

	return Model;
};
